PROCEDURE TForm4.TestShlWAPIPathAddBackslash(Sender : TObject);
VAR pathstrbuf : ARRAY[0.. MAX_PATH - 1] OF CHAR;
VAR wcharpathbufp : PWideChar;
VAR pathstrbuf3 : ARRAY[0..20] OF CHAR;
VAR wcharpathbuf2 : ARRAY[0..22] OF WideChar;
VAR bufsize : INTEGER;
VAR apiretval : POINTER;
VAR lastchar : CHAR;
VAR lastwchar : WideChar;
VAR lastcharaddr : POINTER;
VAR newinfoline : STRING;
BEGIN
FillChar(pathstrbuf, Length(pathstrbuf), #0);
wcharpathbufp := NIL;
FillChar(pathstrbuf3, Length(pathstrbuf3), #0);
ZeroMemory(@wcharpathbuf2, SizeOf(wcharpathbuf2));
bufsize := 0;
apiretval := NIL;
lastchar := #0;
lastwchar := #0;
lastcharaddr := NIL;
newinfoline := '';
newinfoline := 'Example 1.';
Memo1.Lines.Add(newinfoline);
pathstrbuf := 'C:\Hello\World';
newinfoline := 'PathAddBackslash called with ' + pathstrbuf;
Memo1.Lines.Add(newinfoline);
apiretval := PathAddBackslash(pathstrbuf);
IF apiretval <> NIL THEN
newinfoline := 'Returned : 0x' + IntToHex(INTEGER(apiretval), 8) + ' and the modified string : ' + pathstrbuf
ELSE
newinfoline := 'Failed, returning NIL !';
Memo1.Lines.Add(newinfoline);
bufsize := 2 * (Length('C:\Hello\Unicode\World') + 2);
GetMem(wcharpathbufp, bufsize);
ZeroMemory(wcharpathbufp, bufsize);
wcharpathbufp := StringToWideChar('C:\Hello\Unicode\World', wcharpathbufp, bufsize);
newinfoline := 'Example 2.';
Memo1.Lines.Add(newinfoline);
newinfoline := 'PathAddBackslashW called with ' + wcharpathbufp;
Memo1.Lines.Add(newinfoline);
newinfoline := 'The address and size of which are : 0x' + IntToHex(INTEGER(wcharpathbufp), 8);
newinfoline := newinfoline + ' ' + IntToStr(bufsize) + ' bytes';
Memo1.Lines.Add(newinfoline);
apiretval := PathAddBackslashW(wcharpathbufp);
IF apiretval <> NIL THEN
newinfoline := 'Returned : 0x' + IntToHex(INTEGER(apiretval), 8) + ' and the modified Unicode string : ' + wcharpathbufp
ELSE
newinfoline := 'Failed, returning NIL !';
Memo1.Lines.Add(newinfoline);
FreeMem(wcharpathbufp);
//String which already has a trailing backslash
FillChar(pathstrbuf, Length(pathstrbuf), #0);
apiretval := NIL;
bufsize := Length(pathstrbuf);
newinfoline := 'Example 3.';
Memo1.Lines.Add(newinfoline);
pathstrbuf := 'C:\Hello\World\With\Backslash\';
lastchar := pathstrbuf[30];
newinfoline := 'PathAddBackslash called with ' + pathstrbuf + ' and a buffer size of ' + IntToStr(bufsize) + ', ';
Memo1.Lines.Add(newinfoline);
newinfoline := 'the 30th char of which is #' + IntToStr(Ord(lastchar));
Memo1.Lines.Add(newinfoline);
lastcharaddr := @pathstrbuf[30];
newinfoline := 'Address of 30th character : 0x' + IntToHex(INTEGER(lastcharaddr), 8);
Memo1.Lines.Add(newinfoline);
apiretval := PathAddBackslash(pathstrbuf);
IF apiretval <> NIL THEN
BEGIN
newinfoline := 'Returned : 0x' + IntToHex(INTEGER(apiretval), 8) + ' and the buffer contents : '
+ pathstrbuf + ', ';
Memo1.Lines.Add(newinfoline);
lastchar := pathstrbuf[30];
newinfoline := 'the 30th char of which is now "' + lastchar + '"';
END
ELSE
newinfoline := 'Failed, returning NIL !';
Memo1.Lines.Add(newinfoline);
//Buffer too small (should fail, but doesn't !)
apiretval := NIL;
newinfoline := 'Example 4.';
Memo1.Lines.Add(newinfoline);
pathstrbuf3 := 'C:\Hello\World\Four4'; //Text length : 19 characters
bufsize := Length(pathstrbuf3);//Text length + terminating null character
lastchar := pathstrbuf3[20]; //terminating null
newinfoline := 'PathAddBackslash called with ' + pathstrbuf3 + ' and a buffer size of ' + IntToStr(bufsize) + ', ';
Memo1.Lines.Add(newinfoline);
newinfoline := 'the last char of which is #' + IntToStr(Ord(lastchar));
Memo1.Lines.Add(newinfoline);
lastcharaddr := @pathstrbuf3[20]; //address of terminating null
newinfoline := 'Address of last character : 0x' + IntToHex(INTEGER(lastcharaddr), 8);
Memo1.Lines.Add(newinfoline);
apiretval := PathAddBackslash(pathstrbuf3);
IF apiretval <> NIL THEN
BEGIN
newinfoline := 'Returned : 0x' + IntToHex(INTEGER(apiretval), 8) + ' and the modified buffer contents : '
+ pathstrbuf3 + ', ';
Memo1.Lines.Add(newinfoline);
lastchar := pathstrbuf3[20];
newinfoline := 'the last char of which is now "' + lastchar + '"';
END
ELSE
newinfoline := 'Failed, returning NIL !';
Memo1.Lines.Add(newinfoline);
//Buffer too small, Unicode version (should fail, but doesn't !)
apiretval := NIL;
wcharpathbufp := NIL;
bufsize := 0;
newinfoline := 'Example 5.';
Memo1.Lines.Add(newinfoline);
//Text : 'C:\Hello\Unicode\Error' //Text length : 22 characters
bufsize := Length(wcharpathbuf2); //Text length + terminating, null, Unicode character
wcharpathbufp := StringToWideChar('C:\Hello\Unicode\Error', @wcharpathbuf2, bufsize);
lastwchar := wcharpathbuf2[22]; //terminating null
newinfoline := 'PathAddBackslash called with ' + wcharpathbuf2 + ' and a buffer size of ' + IntToStr(bufsize) + ', ';
Memo1.Lines.Add(newinfoline);
newinfoline := 'the last char of which is #' + IntToStr(WORD(lastwchar));
Memo1.Lines.Add(newinfoline);
lastcharaddr := @wcharpathbuf2[22];//address of terminating null
newinfoline := 'Address of last Unicode character : 0x' + IntToHex(INTEGER(lastcharaddr), 8);
Memo1.Lines.Add(newinfoline);
apiretval := PathAddBackslash(pathstrbuf3);
IF apiretval <> NIL THEN
BEGIN
newinfoline := 'Returned : 0x' + IntToHex(INTEGER(apiretval), 8) + ' and the modified buffer contents : '
+ wcharpathbuf2 + ', ';
Memo1.Lines.Add(newinfoline);
lastwchar := wcharpathbuf2[22];
newinfoline := 'the last Unicode char of which is now "' + lastwchar + '"';
END
ELSE
newinfoline := 'Failed, returning NIL !';
Memo1.Lines.Add(newinfoline);
//Buffer size MAX_PATH but still too small (fails !)
apiretval := NIL;
newinfoline := 'Example 6.';
Memo1.Lines.Add(newinfoline);
newinfoline := 'X:\' + StringOfChar('A', 63) + '\' + StringOfChar('b', 63) +
'\' + StringOfChar('C', 63) + '\' + StringOfChar('d', 64);
bufsize := Length(newinfoline);
StrCopy(pathstrbuf, PChar(newinfoline)); //Text length : 259 characters
bufsize := Length(pathstrbuf); //Text length + terminating null character
lastchar := pathstrbuf[MAX_PATH - 1];//terminating null
newinfoline := 'PathAddBackslash called with ' + pathstrbuf + ' and a buffer size of ' + IntToStr(bufsize) + ', ';
Memo1.Lines.Add(newinfoline);
newinfoline := 'the last char of which is #' + IntToStr(Ord(lastchar));
Memo1.Lines.Add(newinfoline);
lastcharaddr := @pathstrbuf[MAX_PATH - 1]; //address of terminating null
newinfoline := 'Address of last character : 0x' + IntToHex(INTEGER(lastcharaddr), 8);
Memo1.Lines.Add(newinfoline);
apiretval := PathAddBackslash(pathstrbuf);
IF apiretval <> NIL THEN
BEGIN
newinfoline := 'Returned : 0x' + IntToHex(INTEGER(apiretval), 8) + ' and the modified buffer contents : '
+ pathstrbuf + ', ';
Memo1.Lines.Add(newinfoline);
lastchar := pathstrbuf[MAX_PATH - 1];
newinfoline := 'the last char of which is now "' + lastchar + '"';
END
ELSE
newinfoline := 'Failed, returning NIL !';
Memo1.Lines.Add(newinfoline);
Memo1.Lines.Add('');
END; |
Example 1.
PathAddBackslash called with C:\Hello\World
Returned : 0x0012F4A7 and the modified string : C:\Hello\World\
Example 2.
PathAddBackslashW called with C:\Hello\Unicode\World
The address and size of which are : 0x012D7EB8 48 bytes
Returned : 0x012D7EE6 and the modified Unicode string : C:\Hello\Unicode\World\
Example 3.
PathAddBackslash called with C:\Hello\World\With\Backslash\ and a buffer size of 260,
the 30th char of which is #0
Address of 30th character : 0x0012F4B6
Returned : 0x0012F4B6 and the buffer contents : C:\Hello\World\With\Backslash\,
the 30th char of which is now "
Example 4.
PathAddBackslash called with C:\Hello\World\Four4 and a buffer size of 21,
the last char of which is #0
Address of last character : 0x0012F497
Returned : 0x0012F498 and the modified buffer contents : C:\Hello\World\Four4\,
the last char of which is now "\"
Example 5.
PathAddBackslash called with C:\Hello\Unicode\Error and a buffer size of 23,
the last char of which is #0
Address of last Unicode character : 0x0012F480
Returned : 0x0012F498 and the modified buffer contents : C:\Hello\Unicode\Error,
the last Unicode char of which is now "
Example 6.
PathAddBackslash called with X:\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd and a buffer size of 260,
the last char of which is #0
Address of last character : 0x0012F59B
Failed, returning NIL ! |